/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import domain.Bankaccount;
import domain.Contract;
import domain.Electricitycontract;
import domain.Insurancecontract;
import domain.Mobilecontract;
import domain.Mortgagecontract;
import domain.Userdom;
import domain.Vehiclecar;
import domain.Watercontract;
import domain.Wcontract;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import planner.Operations;
import util.Validar;

/**
 *
 * @author Alberto
 */
public class Updater extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String type = request.getParameter("type");
            Userdom u = (Userdom) request.getSession().getAttribute("user");
            Operations o = new Operations();



            if (type.equals("user")) {
                String nick = request.getParameter("nick");
                String pass = request.getParameter("pass");
                String name = request.getParameter("name");
                String surname = request.getParameter("surname");
                String date = request.getParameter("date");
                Date fecha = java.sql.Date.valueOf(date);

                Userdom uMod = new Userdom(nick, pass, name, surname, fecha);

                request.getSession().setAttribute("user", uMod);
                new Operations().updateUser(uMod);

                u.setName(name);
                u.setPass(pass);
                u.setSurname(surname);
            } else if (type.equals("modifyCar")) {
                String neumdelizq = request.getParameter("Neumatico Delantero Izquierdo");
                String neumdelder = request.getParameter("Neumatico Delantero Derecho");
                String neumtrasizq = request.getParameter("Neumatico Trasero Izquierdo");
                String neumtrasder = request.getParameter("Neumatico Trasero Derecho");

                String liqFrenos = request.getParameter("Liquido de Frenos");
                String pastFrenosDel = request.getParameter("Pastillas de Frenos Delanteros");
                String pastFrenosTras = request.getParameter("Pastillas de Frenos Traseros");
                String discfrenos = request.getParameter("Discos de Frenos");

                String sintetico = request.getParameter("Sintetico");
                String semisintetico = request.getParameter("Semisintetico");
                String mineral = request.getParameter("Mineral");

                String cableBujias = request.getParameter("Cables de Bujias");
                String bujias = request.getParameter("Bujias");

                String liqLimpia = request.getParameter("Liquido Limpiaparabrisas");
                String limpiaDel = request.getParameter("Limpiaparabrisas Delanteros");
                String limpiaTras = request.getParameter("Limpiaparabrisas Trasero");

                String amortiguacion = request.getParameter("Amortiguacion");
                String liqRefrig = request.getParameter("Liquido Refrigerante");
                String filtroAire = request.getParameter("Filtro de Aire");
                String bateria = request.getParameter("Bateria");
                String aceiteCaja = request.getParameter("Aceite Caja de Cambios");
                String correaDist = request.getParameter("Correa de Distribucion");
                String filtroComb = request.getParameter("Filtro de Combustible");
                String airbag = request.getParameter("Airbag");

                String lucesPos1 = request.getParameter("Luces de Posicion1");
                String lucesCruce1 = request.getParameter("Luces de Cruce1");
                String lucesCarr1 = request.getParameter("Luces de Carretera1");
                String lucesPos2 = request.getParameter("Luces de Posicion2");
                String lucesCruce2 = request.getParameter("Luces de Cruce2");
                String lucesCarr2 = request.getParameter("Luces de Carretera2");
                String lucesPos3 = request.getParameter("Luces de Posicion3");
                String lucesCruce3 = request.getParameter("Luces de Cruce3");
                String lucesCarr3 = request.getParameter("Luces de Carretera3");
                String lucesFreno1 = request.getParameter("Luces de Freno1");
                String lucesMarcha1 = request.getParameter("Luces de Marcha Atras1");
                String lucesPos4 = request.getParameter("Luces de Posicion4");
                String lucesCruce4 = request.getParameter("Luces de Cruce4");
                String lucesCarr4 = request.getParameter("Luces de Carretera4");
                String lucesFreno2 = request.getParameter("Luces de Freno2");
                String lucesMarcha2 = request.getParameter("Luces de Marcha Atras2");

                Vehiclecar v = new Vehiclecar(u, "", "", "", neumdelizq, neumdelder, neumtrasizq, neumtrasder, liqFrenos, pastFrenosDel, pastFrenosTras, discfrenos, sintetico, semisintetico, mineral, cableBujias, bujias, liqLimpia, limpiaDel, limpiaTras, amortiguacion, liqRefrig, filtroAire, bateria, aceiteCaja, correaDist, filtroComb, airbag, lucesPos1, lucesCruce1, lucesCarr1, lucesPos2, lucesCruce2, lucesCarr2, lucesPos3, lucesCruce3, lucesCarr3, lucesFreno1, lucesMarcha1, lucesPos4, lucesCruce4, lucesCarr4, lucesFreno2, lucesMarcha2);
                Integer idCar = Integer.parseInt(request.getParameter("idCar"));
                v.setIdCar(idCar);
                new Operations().updateVehiclecar(v);

                request.getSession().setAttribute("vehicle", v);

                o.getVehiclecar(idCar).setAceitecajacambios(aceiteCaja);
                o.getVehiclecar(idCar).setAirbag(airbag);
                o.getVehiclecar(idCar).setAtrastrasder(neumtrasder);
                o.getVehiclecar(idCar).setAtrastrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setBateria(bateria);
                o.getVehiclecar(idCar).setBujia(bujias);
                o.getVehiclecar(idCar).setCablebujia(cableBujias);
                o.getVehiclecar(idCar).setCarreteradelder(neumdelder);
                o.getVehiclecar(idCar).setCarreteradelizq(neumdelizq);
                o.getVehiclecar(idCar).setCarreteratrasder(neumtrasder);
                o.getVehiclecar(idCar).setCarreteratrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setCorreadist(correaDist);
                o.getVehiclecar(idCar).setCrucedelder(neumdelder);
                o.getVehiclecar(idCar).setCrucedelizq(neumdelizq);
                o.getVehiclecar(idCar).setCrucetrasder(neumtrasder);
                o.getVehiclecar(idCar).setCrucetrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setDiscofreno(discfrenos);
                o.getVehiclecar(idCar).setFiltroaire(filtroAire);
                o.getVehiclecar(idCar).setFiltrocombust(filtroComb);
                o.getVehiclecar(idCar).setFrenotrasder(neumtrasder);
                o.getVehiclecar(idCar).setFrenotrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setLimpiadel(limpiaDel);
                o.getVehiclecar(idCar).setLimpiatras(limpiaTras);
                o.getVehiclecar(idCar).setLiquidlimpia(liqLimpia);
                o.getVehiclecar(idCar).setLiquidofrenos(liqFrenos);
                o.getVehiclecar(idCar).setLiquidrefrigerante(liqRefrig);
                o.getVehiclecar(idCar).setMark(type);
                o.getVehiclecar(idCar).setMineral(mineral);
                o.getVehiclecar(idCar).setNeumdelder(neumdelder);
                o.getVehiclecar(idCar).setNeumdelizq(neumdelizq);
                o.getVehiclecar(idCar).setNeumtrasder(neumtrasder);
                o.getVehiclecar(idCar).setNeumtrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setOwner(mineral);
                o.getVehiclecar(idCar).setPasfrentras(pastFrenosTras);
                o.getVehiclecar(idCar).setPastfrendel(pastFrenosDel);
                o.getVehiclecar(idCar).setPosiciondelder(neumdelder);
                o.getVehiclecar(idCar).setPosiciondelizq(neumdelizq);
                o.getVehiclecar(idCar).setPosiciontrasder(neumtrasder);
                o.getVehiclecar(idCar).setPosiciontrasizq(neumtrasizq);
                o.getVehiclecar(idCar).setSemisintetico(semisintetico);
                o.getVehiclecar(idCar).setSintetico(sintetico);

            } else if (type.equals("electricityCont")) {

                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String idCo = request.getParameter("idCon");
                Integer id = Integer.parseInt(idCo);
                String compania = request.getParameter("compania");
                String date = request.getParameter("fecha");
                String potencia = request.getParameter("potencia");
                String name = request.getParameter("name");

                Date fecha = Date.valueOf(date);
                Double power = Double.valueOf(potencia);

                if (Validar.esValidoChar(compania) && Validar.esValidoChar(name) && Validar.esValidoDecimal(potencia) && Validar.esValidoNum(idBank)) {

                    Contract c = new Contract(new Operations().getBankAccount(idB), fecha, true, name);
                    c.setIdContract(id);
                    Electricitycontract e = new Electricitycontract(c, compania, power);
                    Integer idC = Integer.parseInt(request.getParameter("idC"));
                    e.setIdElectricityContract(idC);

                    o.updateElecContract(e, c);


                    u.getBankaccount(idB).getContract(id).setBankaccount(o.getBankAccount(idB));
                    u.getBankaccount(idB).getContract(id).setDateContract(fecha);
                    u.getBankaccount(idB).getContract(id).setNameContract(name);

                    o.getElectricityContract(idC).setCompany(compania);
                    o.getElectricityContract(idC).setContratedPower(power);

                }
            } else if (type.equals("waterCont")) {
                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String date = request.getParameter("fecha");
                String name = request.getParameter("name");

                String idCo = request.getParameter("idCon");
                Integer id = Integer.parseInt(idCo);

                if (Validar.esValidoChar(name) && Validar.esValidoNum(idBank)) {
                    Date fecha = Date.valueOf(date);

                    Contract c = new Contract(new Operations().getBankAccount(idB), fecha, true, name);
                    c.setIdContract(id);
                    Watercontract w = new Watercontract(c, name);
                    Integer idC = Integer.parseInt(request.getParameter("idC"));
                    w.setIdWaterContract(idC);

                    new Operations().updateWaterContract(w, c);


                    u.getBankaccount(idB).getContract(id).setBankaccount(o.getBankAccount(idB));
                    u.getBankaccount(idB).getContract(id).setDateContract(fecha);
                    u.getBankaccount(idB).getContract(id).setNameContract(name);

                    o.getWaterContract(idC).setCompany(name);

                }
            } else if (type.equals("mortgageCont")) {
                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String date = request.getParameter("fecha");
                String dateExp = request.getParameter("fechaExp");
                String t = request.getParameter("total");
                String i = request.getParameter("interes");
                String name = request.getParameter("name");

                String idCo = request.getParameter("idCon");
                Integer id = Integer.parseInt(idCo);


                if (Validar.esValidoChar(name) && Validar.esValidoDecimal(i) && Validar.esValidoDecimal(t) && Validar.esValidoNum(idBank)) {
                    Date fechaExp = Date.valueOf(dateExp);
                    Date fecha = Date.valueOf(date);
                    double total = Double.valueOf(t);
                    double interes = Double.valueOf(i);
                    Contract c = new Contract(new Operations().getBankAccount(idB), fecha, true, name);
                    c.setIdContract(id);
                    Mortgagecontract m = new Mortgagecontract(c, fechaExp, name, total, interes);
                    Integer idC = Integer.parseInt(request.getParameter("idC"));
                    m.setIdMortgageContract(idC);

                    new Operations().updateMortgageContract(m, c);


                    u.getBankaccount(idB).getContract(id).setBankaccount(o.getBankAccount(idB));
                    u.getBankaccount(idB).getContract(id).setDateContract(fecha);
                    u.getBankaccount(idB).getContract(id).setNameContract(name);

                    o.getMortgageContract(idC).setCompany(name);
                    o.getMortgageContract(idC).setDateOfExpiration(fechaExp);
                    o.getMortgageContract(idC).setInterest(interes);
                    o.getMortgageContract(idC).setTotal(total);
                }
            } else if (type.equals("insuranceCont")) {
                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String coment = request.getParameter("coment");
                String date = request.getParameter("fecha");
                String dateExp = request.getParameter("fechaExp");
                String name = request.getParameter("name");

                String idCo = request.getParameter("idCon");
                Integer id = Integer.parseInt(idCo);

                if (Validar.esValidoChar(coment) && Validar.esValidoChar(name) && Validar.esValidoNum(idBank)) {
                    Date fecha = Date.valueOf(date);
                    Date fechaExp = Date.valueOf(dateExp);
                    Contract c = new Contract(new Operations().getBankAccount(idB), fecha, true, name);
                    c.setIdContract(id);
                    Insurancecontract i = new Insurancecontract(c, fechaExp, coment);
                    Integer idC = Integer.parseInt(request.getParameter("idC"));
                    i.setIdInsuranceContract(idC);

                    new Operations().updateInsuranceContract(i, c);

                    u.getBankaccount(idB).getContract(id).setBankaccount(o.getBankAccount(idB));
                    u.getBankaccount(idB).getContract(id).setDateContract(fecha);
                    u.getBankaccount(idB).getContract(id).setNameContract(name);

                    o.getInsuranceContract(idC).setCompany(name);
                    o.getInsuranceContract(idC).setDateOfExpiration(fechaExp);
                }
            } else if (type.equals("mobileCont")) {
                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String number = request.getParameter("numero");
                Integer numero = Integer.parseInt(number);
                String compania = request.getParameter("compania");
                String date = request.getParameter("fecha");
                String perm = request.getParameter("permanencia");

                String idCo = request.getParameter("idCon");
                Integer id = Integer.parseInt(idCo);
                if (Validar.esValidoChar(perm) && Validar.esValidoNum(idBank) && Validar.esValidoNum(number) && Validar.esValidoChar(compania)) {
                    Date fecha = Date.valueOf(date);

                    Integer pemanencia = Integer.parseInt(perm);
                    Contract c = new Contract(new Operations().getBankAccount(idB), fecha, true, number);
                    c.setIdContract(id);
                    Mobilecontract m = new Mobilecontract(c, compania, pemanencia, numero);
                    Integer idC = Integer.parseInt(request.getParameter("idC"));
                    m.setIdMobileContract(idC);

                    new Operations().updateMobileContract(m, c);


                    u.getBankaccount(idB).getContract(id).setBankaccount(o.getBankAccount(idB));
                    u.getBankaccount(idB).getContract(id).setDateContract(fecha);
                    u.getBankaccount(idB).getContract(id).setNameContract(compania);

                    o.getMobileContract(idC).setCompany(compania);
                    o.getMobileContract(idC).setMobileNumber(numero);
                    o.getMobileContract(idC).setPermanence(pemanencia);
                }
            } else if (type.equals("wCont")) {
                String idBank = request.getParameter("bank");
                Integer idB = Integer.parseInt(idBank);
                String workName = request.getParameter("workName");
                String salary = request.getParameter("basicSalary");



                Double basicSalary = Double.valueOf(salary);
                Wcontract wc = new Wcontract(new Operations().getBankAccount(idB), workName, basicSalary);
                wc.setIdWcontract(Integer.parseInt(request.getParameter("idC")));
                new Operations().updateWContract(wc);


            } else if (type.equals("bankAccount")) {

                String id = request.getParameter("idC");
                String nombre = request.getParameter("nombre");
                Integer idB = Integer.parseInt(id);

                if (Validar.esValidoChar(nombre)) {

                    Bankaccount b = new Bankaccount(u, nombre);

                    o.updateBankAccount(b);

                    u.getBankaccount(idB).setName(nombre);

                }
            }

        } finally {
            String redir = request.getParameter("redir");

            RequestDispatcher d = request.getRequestDispatcher("./FrontController?redir=" + redir);
            d.forward(request, response);
            out.close();

        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
